package wholemusic.web.watch;

import com.wolfram.alpha.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class WolframAlphaAPI {

    @SuppressWarnings("SpellCheckingInspection")
    private static final String APP_ID = "HYWLVR-3UP46XAUH3";

    public static WAQueryResult query(String input) {
        // The WAEngine is a factory for creating WAQuery objects,
        // and it also used to perform those queries. You can set properties of
        // the WAEngine (such as the desired API output format types) that will
        // be inherited by all WAQuery objects created from it. Most applications
        // will only need to crete one WAEngine object, which is used throughout
        // the life of the application.
        WAEngine engine = new WAEngine();

        // These properties will be set in all the WAQuery objects created from this WAEngine.
        engine.setAppID(APP_ID);
        // engine.addFormat("plaintext");
        WAQuery query = engine.createQuery();

        // Set properties of the query.
        query.setInput(input);

        try {
            // For educational purposes, print out the URL we are about to send:
            System.out.println("Query URL:");
            System.out.println(engine.toURL(query));
            System.out.println();
            // This sends the URL to the Wolfram|Alpha server, gets the XML result
            // and parses it into an object hierarchy held by the WAQueryResult object.
            return engine.performQuery(query);
        } catch (WAException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage queryImage(WAQueryResult queryResult) {
        if (queryResult != null) {
            if (queryResult.isError()) {
                System.out.println("Query error");
                System.out.println("  error code: " + queryResult.getErrorCode());
                System.out.println("  error message: " + queryResult.getErrorMessage());
            } else if (!queryResult.isSuccess()) {
                System.out.println("Query was not understood; no results available.");
            } else {
                // Got a result.
                System.out.println("Successful query. Pods follow:\n");
                for (WAPod pod : queryResult.getPods()) {
                    if (!pod.isError()) {
                        System.out.println(pod.getTitle());
                        System.out.println("------------");
                        if (pod.getTitle() != null && pod.getTitle().toLowerCase().contains("input")) {
                            System.out.println("------------ ignore input pod, continue;");
                            continue;
                        }
                        for (WASubpod subPod : pod.getSubpods()) {
                            for (Object element : subPod.getContents()) {
                                if (element instanceof WAPlainText) {
                                    System.out.println(((WAPlainText) element).getText());
                                    System.out.println();
                                } else if (element instanceof WAImage) {
                                    WAImage image = (WAImage) element;
                                    System.out.println(image.getURL());
                                    System.out.println(image.getTitle());
                                    image.acquireImage();
                                    if (image.getFile() != null) {
                                        try {
                                            BufferedImage img = ImageIO.read(image.getFile());
                                            return img;
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }
                        }
                        System.out.println();
                    }
                }
            }
        }
        return null;
    }
}
